// pages/shangpin/shangpin.js
const footer = require('../footer/footer.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelect: '3',
    shangpin: [],
    shangpinXianshi: [],
    searchValue: ""
  },
valueSave:function (params) {
  var value = params.detail.value
  this.setData({
    searchValue:value
  })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.shangpinXianshi == null || options.shangpin == null) {
      var that = this
    var req = wx.request({
      url: 'https://127.0.0.1:10270/api/Return2WeiXin?userId=1&type=4',
      method: "post",
      success: (result) => {
        that.setData({
          shangpin: result.data.kucunguanliReturnList,
          shangpinXianshi: result.data.kucunguanliReturnList
        })
        req.abort()
      },
      fail: (err) => {},
    })
    }else{
      this.setData({
        shangpin:options.shangpin,
        shangpinXianshi:options.shangpinXianshi
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  pageReSetClick: function (params) {
    footer.pageReSetClick(params)
  },
  TiaomaDayin:function (params) {
    var id = params.currentTarget.dataset.id
    this.data.shangpinXianshi.forEach((item)=>{
      if (item.id == id) {
        app.globalData.logShangpin = []
        app.globalData.logShangpin.push(item) 
        wx.navigateTo({
          url: '/pages/console/console',
        })
      }
    })
  },
  searchValue:function (params) {
    var value = params.detail.value
    console.log(value);
    if (value == null || value.length == 0 || value == undefined) {
      this.onLoad({shangpinXianshi: this.data.shangpin,shangpin: this.data.shangpin})
    }
    var newArr = []
    this.data.shangpin.forEach(element => {
      if (element.shangpinming.indexOf(value) != -1 || 
        element.shangpinguigegao == value  || 
        element.shangpinguigekuan == value  || 
        value.indexOf(element.shangpinkaixiang) != -1 ) {
        newArr.push(element)
      }
    });
    this.onLoad({shangpinXianshi: newArr,shangpin: this.data.shangpin})
  }
})